//
//  PywSetPwdApi.m
//  PywSdk
//
//  Created by smallwave on 16/8/9.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywSetPwdApi.h"
#import "AIFSetPwdManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PropertyListReformer.h"

@interface PywSetPwdApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywSetPwdApi

- (void)requestSetPwdWithParams:(MutableDictionary *)params
                        success:(Successd)success
                           fail:(Failure)fail{
    
    self.successd = success;
    self.failure = fail;
    
    AIFSetPwdManager *manager = [AIFSetPwdManager manager];
    manager.passport = [params objectForKey:@"passport"];
    manager.password = [params objectForKey:@"password"];
    manager.smscode = [params objectForKey:@"smscode"];
    manager.smstype = [params objectForKey:@"smstype"];
    manager.is_check_smscode = [params objectForKey:@"is_check_smscode"];
    manager.delegate = self;
    [manager loadData];
    
}

#pragma mark - ETAPIManagerApiCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    [self resetPwdWithDict:dict];
}
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywMBProgressHUD showError:@"设置密码失败,请重新设置" toView:[UIApplication sharedApplication].keyWindow];
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}
/**
 *  重置密码成功
 */
- (void)resetPwdWithDict:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    switch (resultCode) {
            case 200:
        {
            [PywMBProgressHUD showSuccess:@"重置成功，请重新登录！" toView:[UIApplication sharedApplication].keyWindow];
            if (self.successd) {
                self.successd(nil,nil);
            }
        }
            break;
     
        default:
        {
            NSString *msg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:msg toView:[UIApplication sharedApplication].keyWindow];
            if (self.failure) {
                self.failure([self errorWithCode:resultCode]);
            }
        }
            break;
    }
}


@end
